package com.icarsaas.saas.tenant.system.web;

import com.icarsaas.saas.common.page.SoulPage;
import com.icarsaas.saas.common.utils.ResultVoUtil;
import com.icarsaas.saas.common.vo.ResultVo;
import com.icarsaas.saas.tenant.system.model.ActionLog;
import com.icarsaas.saas.tenant.system.service.ActionLogService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller @Api(tags="日志模块")
@RequestMapping("/system/actionLog")
public class ActionLogController {

    @Autowired
    private ActionLogService actionLogService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("system:actionLog:index")
    public String index(Model model, ActionLog actionLog){

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();

        // 获取日志列表
        Example<ActionLog> example = Example.of(actionLog, matcher);
        Page<ActionLog> list = actionLogService.getPageList(example);

        // 封装数据
        model.addAttribute("list",list.getContent());
        model.addAttribute("page",list);
        return "/system/actionLog/index";
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:actionLog:detail")
    public String toDetail(@PathVariable("id") ActionLog actionLog, Model model){
        model.addAttribute("actionLog",actionLog);
        return "/system/actionLog/detail";
    }

    /**
     * 删除指定的日志
     */
    @SuppressWarnings("rawtypes")
	@PostMapping("/status/delete")
    @RequiresPermissions("system:actionLog:delete")
    @ResponseBody
    public ResultVo delete(@RequestParam(value = "ids", required = false) Long id){
        if (id != null && id != -1L) {
            actionLogService.deleteId(id);
            return ResultVoUtil.success("删除日志成功");
        } else {
            actionLogService.emptyLog();
            return ResultVoUtil.success("清空日志成功");
        }
    }
    
  //---------------------------新的页面方法----------------------//
    /**
     * 跳转到列表页面
     */
    @GetMapping("/logList") @RequiresPermissions("system:actionLog:index")
    public String logList() {
        return "/system/actionLog/logList";
    }
    /**
     * 获取数据
     * @return
     */
    @PostMapping("getList") @ResponseBody
	public Object getList(SoulPage<Map<String, Object>> page, @RequestBody Map<String, Object> params) {
    	page.setParams(params, page);
		page = actionLogService.listData(page, params);
		return page;
    }
    
    /**
     * 删除指定的日志
     */
	@PostMapping("/status/deleteEntity")
    @RequiresPermissions("system:actionLog:delete") @ResponseBody
    public ResultVo<?> deleteEntity(@RequestParam(value = "ids", required = false) List<Long> ids) {
		if (ids != null) {
			for (Long id : ids) {
				actionLogService.deleteId(id);
			}
		}
		return ResultVoUtil.success("删除日志成功");
		/*
        if (id != null && id != -1L) {
            actionLogService.deleteId(id);
            return ResultVoUtil.success("删除日志成功");
        } else {
            actionLogService.emptyLog();
            return ResultVoUtil.success("清空日志成功");
        }
        */
    }
    
}
